package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.annotation.RequiredLogin;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.service.IStrategyStatisVoRedisService;
import cn.wolfcode.wolf2w.service.IUserInfoRedisService;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import cn.wolfcode.wolf2w.util.AssertUtil;
import cn.wolfcode.wolf2w.util.JsonResult;
import cn.wolfcode.wolf2w.util.LogicException;
import cn.wolfcode.wolf2w.util.RedisKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("users")
public class UserInfoController {

    @Autowired
    private IUserInfoService userInfoService;
    @Autowired
    private IUserInfoRedisService userInfoRedisService;
    @Autowired
    private StringRedisTemplate template;
    @Autowired
    private IStrategyStatisVoRedisService strategyStatisVoRedisService;

    @GetMapping("/get")
    public Object get(){
        return userInfoService.get("5e295f01a00e265228f963ea");
    }

    /**
     * 判断手机是否已注册接口
     * @param phone 用户手机号码
     * @return
     */
    @GetMapping("/checkPhone")
    public Object checkPhone(String phone){
        // 接收用户传过来手机号码
        boolean ret = userInfoService.checkPhone(phone);
        // 判断是否已经被注册
        return !ret;
    }

    /**
     * 发送验证码接口
     * @param phone 用户手机号码
     * @return
     */
    @GetMapping("/sendVerifyCode")
    public Object sendVerifyCode(String phone){

        // 给用户手机号发送验证码

        userInfoRedisService.sendVerifyCode(phone);

        // 返回JsonResult让前端判断发送结果
        return JsonResult.success();
    }


    /**
     * 完成注册接口
     * @param userInfo 封装用户注册信息,[手机号,昵称,密码]
     * @param rpassword 第二次输入的密码
     * @param verifyCode 用户输入的验证码
     * @return
     */
    @PostMapping("/regist")
    public Object regist(UserInfo userInfo, String rpassword,String verifyCode){

        // 从redis中拿出验证码
        String s = template.opsForValue().get(RedisKey.VERIFY_CODE.join(userInfo.getPhone()));

        // 判断用户输入的数据是否为空
        AssertUtil.hasLength(userInfo.getNickname(), "请输入昵称");
        AssertUtil.hasLength(userInfo.getPassword(), "请输入密码");
        AssertUtil.hasLength(rpassword, "请再次输入密码");
        AssertUtil.hasLength(verifyCode, "请输入验证码");


        // 判断昵称是否已被注册
        if (userInfoService.nicknameExist(userInfo.getNickname())){
            // 如果已被注册
            throw new LogicException("该用户已经被注册");
        }

        userInfoService.checkPhone(userInfo.getPhone());

        // 判断两次输入的密码是否一致
        AssertUtil.isEqual(userInfo.getPassword(), rpassword, "两次输入的密码不一致");


        userInfo.setEmail("");
        userInfo.setCity("");
        userInfo.setHeadImgUrl("/images/default.jpg");
        userInfo.setInfo("");
        userInfo.setState(UserInfo.STATE_NORMAL);//核心属性需要由我们自己控制,所以即使有默认值也要给他设置初始值

        // 如果输入的验证码没错的话
        if (verifyCode.equals(s)){
            // 返回200的JsonResult
            userInfoService.save(userInfo);
            return JsonResult.success();
        }else {
            throw new LogicException("验证码错误");
        }
    }

    /**
     * 用户登录接口
     * 检查登录用户的账号密码是否有异常
     * 把成功登录的用户缓存到redis中
     * 把token和用户对象返回(通过JsonResult)
     */
    @PostMapping("/login")
    public Object login(String username,String password){

        UserInfo user = userInfoService.login(username,password);

        String token = userInfoRedisService.setToken(user);

        // 创建一个map来封装user 和 token
        Map<String,Object> map = new HashMap<>();
        map.put("token", token);
        map.put("user", user);

        return JsonResult.success(map);
    }

    /**
     * 通过令牌获取登录对象
     * @param request   请求对象
     * @return  已登录用户
     */
    @GetMapping("/currentUser")
    @RequiredLogin
    public Object currentUser(HttpServletRequest request){
        String token = request.getHeader("token");
        System.out.println(token);
        UserInfo currentUser = userInfoRedisService.getToken(token);
        System.out.println(currentUser);
        return JsonResult.success(currentUser);
    }


    @GetMapping("/strategies/favor")
    public Object favor(String sid,String userId){
        List<String> sids =strategyStatisVoRedisService.getFavor(sid,userId);
        return JsonResult.success(sids);
    }
}
